<?php
class RM_PickupDropoffPrices extends RM_Model
{
    protected $_name = 'rm_pickupdropoff_prices';

    /**
     * Get the price by location ids
     * 
     * @param int $locations_a
     * @param int $location_b
     * @return object
     */
    public function getPriceByLocationIDs($locations_a, $location_b){
        $where = "location_id_a='".$locations_a."' AND location_id_b='".$location_b."'";
        $data = $this->fetchAll($where)->toArray();
        if (isset ($data[0])){
            $data = $data[0];
        }

        if (!isset($data['price'])) {
            // if there is no price rule/data for these locations, return the default price.
            $config = new RM_PickupDropoffConfig();
            $configData = $config->get()->toArray();$configData = $configData[0];
            $data = array("price"=>$configData['default_price']);
        }
        return $data;
    }

    /**
     * Calculate the total price for the p/u locations
     *
     * @param object $locationData
     * @return float
     */
    public function calculate($locationData)
    {

        if (!isset($locationData) && !$locationData->id_pickup && $locationData->id_dropoff){
            return FALSE;
        }
        $price = $this->getPriceByLocationIDs($locationData->id_pickup, $locationData->id_dropoff);
        return $price;
    }

}